package com.drop.service.impl;

import com.drop.common.*;
import com.drop.dao.CommonDao;
import com.drop.dao.GoodsDao;
import com.drop.entity.Goods;
import com.drop.entity.Goodstype;
import com.drop.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈东一 on 2017/9/23 9:42
 */
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	private CommonDao commonDao;
	
	@Autowired
	private GoodsDao goodsDao;

	@Override
	public Goods showGoods(int id) {
		Goods goods= (Goods) commonDao.getById(Goods.class, id);
		System.out.println(goods);
		return goods;
	}

	@Override
	public List<Goods> searchGoodsByTerm(String str) {
		return goodsDao.getGoodsByTerm(str);
	}


	@Override
	public MyType showGoodsType() throws Exception {
		List<Goodstype> types =  (List<Goodstype>)(List)commonDao.getAll(Goodstype.class);
		MyType myType = new MyType();
		List<MyGoods> goods;
		for(int i=0; i<types.size(); i++) {
			String type = types.get(i).getTypeContent();
			goods = new ArrayList<MyGoods>();
			for(Goods g : types.get(i).getGoodses()){
				goods.add(new MyGoods(g.getGoodsName(),g.getGoodsId().toString()));
			}
			myType.getType().add(type);
			myType.getGoods().put(type, goods);
		}
		return myType;
	}

	@Override
	public EasyUIDataGridResult showAllGoods(int shipid,String page,String rows) {
		EasyUIDataGridResult e = new EasyUIDataGridResult();
		List<Goods> goods = (List<Goods>)(List)commonDao.getByPage(Goods.class,Integer.parseInt(page),Integer.parseInt(rows));
		List<MyGoods1> goods1s = new ArrayList<>();
		e.setTotal(commonDao.count(Goods.class));
		for (Goods g : goods){
			goods1s.add(new MyGoods1(g.getGoodsId(), g.getGoodstype().getTypeContent(),g.getGoodsName(), g.getGoodsImg(), g.getGoodsContent()
					, g.getPrice(), g.getStock(), g.getSalesNum()));
		}
		e.setRows(goods1s);
		return e;
	}

	@Override
	public MyCart addGoods(String goodstype, String goodsName, String goodsContent, String price, String stock,
						   String goodsImg, int shipid) {
		goodsDao.insertGoods(goodstype, goodsName, goodsContent, price, stock, goodsImg, shipid, 0);
		return new MyCart("true","添加商品成功");
	}

	@Override
	public MyCart updateGoods(String goodsId, String goodstype, String goodsName, String goodsContent, String price,
							  String stock, String goodsImg, int i) {
		Goods goods = (Goods) commonDao.getById(Goods.class, Integer.valueOf(goodsId));
		Goodstype type = (Goodstype) commonDao.getById(Goodstype.class, Integer.valueOf(goodstype));
		goods.setGoodsName(goodsName);
		goods.setGoodsContent(goodsContent);
		goods.setGoodstype(type);
		goods.setPrice(Float.valueOf(price));
		goods.setStock(Integer.valueOf(stock));
		goods.setGoodsImg(goodsImg);
		commonDao.update(goods);
		return  new MyCart("true","修改商品成功");
	}

	@Override
	public MyCart deleteGoods(String goodsId) {
		commonDao.delete(commonDao.getById(Goods.class, Integer.valueOf(goodsId)));
		return new MyCart("true","删除商品成功");
	}
}
